<!-- 新增组别 -->
<template>
  <el-dialog
    class="gl_dialog_TitleLine"
    width="980px"
    :title="title"
    :append-to-body="true"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form
      ref="eventGroupsVO"
      :model="eventGroupsVO"
      :rules="eventGroupsVO_Rules"
      label-width="100px"
      size="mini"
      @submit.native.prevent
    >
      <el-row :gutter="15">
        <el-col :span="24" class="form_item">
          <el-form-item label="组别名称" prop="name">
            <el-input
              v-model.trim="eventGroupsVO.name"
              placeholder="请输入组别名称"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="15">
        <el-col :span="13" class="form_item">
          <el-form-item label="年龄区间" prop="startBirthDate">
            <el-date-picker
              v-model="eventGroupsVO.startBirthDate"
              type="date"
              style="width: 100%"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              placeholder="开始日期"
            />
          </el-form-item>
        </el-col>
        <el-col :span="11" class="form_item">
          <el-form-item label="" label-width="0" prop="endBirthDate">
            <el-date-picker
              v-model="eventGroupsVO.endBirthDate"
              type="date"
              style="width: 100%"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              placeholder="结束日期"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer">
      <el-button type="default" size="mini" @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" size="mini" :loading="submitLoading" @click="submitGroupInfo()">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { mapState, mapActions } from 'vuex';
import {
  api_PUT_EventsManagementEventGroupId
} from '@/extend/api/gameswaggerApi/app';
import { SiSTKWEventsManagementEventGroupUpdateDto } from '@/lib/form/game/swaggerForm';
import { SiSTKWEventsManagementEventGroupDTO_Rules } from '@/lib/validate/events';
import { CopyData } from '@/lib';
export default {
  data() {
    return {
      title: '修改项目组别',
      dialogVisible: false,
      submitLoading: false,
      eventGroupsVO: new SiSTKWEventsManagementEventGroupUpdateDto(),
      eventGroupsVO_Rules: new SiSTKWEventsManagementEventGroupDTO_Rules()
    };
  },
  computed: {
    ...mapState({
      dict: state => state.dict.dict
    })
  },
  created() {
    this.getDict();
    this.initRules();
  },
  methods: {
    ...mapActions({ getDict: 'dict/getDict' }),
    open(eventGroups) {
      console.log(eventGroups);
      this.submitLoading = false;
      this.eventGroups = eventGroups;
      this.eventGroupsVO = new SiSTKWEventsManagementEventGroupUpdateDto();
      CopyData(this.eventGroups, this.eventGroupsVO);
      this.dialogVisible = true;
    },
    initRules() {
      const _this = this;
      this.eventGroupsVO_Rules.startBirthDate.push({
        validator(rule, value, callback) {
          const _startBirthDate = value && new Date(value) || '';
          const _endBirthDate = _this.eventGroupsVO.endBirthDate && new Date(_this.eventGroupsVO.endBirthDate.substring(0, 10)) || '';
          if (_startBirthDate && _endBirthDate && _startBirthDate > _endBirthDate) {
            callback(new Error('开始日期不能大于结束日期'));
          }
          callback();
        },
        trigger: 'change'
      });
      this.eventGroupsVO_Rules.endBirthDate.push({
        validator(rule, value, callback) {
          const _startBirthDate = _this.eventGroupsVO.startBirthDate && new Date(_this.eventGroupsVO.startBirthDate) || '';
          const _endBirthDate = value && new Date(value) || '';
          if (_startBirthDate && _endBirthDate && _startBirthDate > _endBirthDate) {
            callback(new Error('结束日期不能小于开始日期'));
          }
          callback();
        },
        trigger: 'change'
      });
    },
    // 提交
    submitGroupInfo() {
      this.$refs.eventGroupsVO.validate(valid => {
        if (!valid) return;
        this.eventGroupsVO.startBirthDate = this.eventGroupsVO.startBirthDate && this.eventGroupsVO.startBirthDate.substring(0, 10);
        this.eventGroupsVO.endBirthDate = this.eventGroupsVO.endBirthDate && this.eventGroupsVO.endBirthDate.substring(0, 10);
        this.submitLoading = true;
        api_PUT_EventsManagementEventGroupId(this.eventGroups.id, this.eventGroupsVO).then(res => {
          this.$message({ type: 'success', message: '修改成功！' });
          this.submitLoading = false;
          this.dialogVisible = false;
          this.$emit('refreshPage');
        }, error => {
          this.submitLoading = false;
          this.$message({ type: 'error', message: error.error && error.error.message || '' });
        });
      });
    }
  }
};
</script>
